AdminCon = iClass("Admin");
var role = require("../lib/role");
module.exports = {
	prefix: "/admin",
	get: {
		"/": function() {
			var admin = role.getAdmin(this);
			this.body = ResponObj("json", admin);
		},
		"/loginout": function() {
			try {
				var admin = role.getAdmin(this);
				role.removeAdmin(this, admin);
				this.body = ResponObj("json", "管理员下线成功");
			} catch (errorMsg) {
				this.body = ResponObj("error", errorMsg);
			}
		},
	},
	post: {
		"/register": function() {
			var admin_info = this.request.body;
			if (!(admin_info.password && admin_info.confirm_password)) {
				throw "表单不完整"
			} else if (admin_info.password !== admin_info.confirm_password) {
				throw "两次密码输入不匹配"
			} else {
				var new_admin = new AdminCon(admin_info);
				this.body = ResponObj("json", new_admin);
			}
		},
		//管理员登录
		"/login": function() {
			var query = this.request.query;
			if (query.from_bus) { //使用已经登录的商家身份进行登录
				var bus = role.getBus(this);
				var admin = bus.asAdmin();
			} else {
				var form_data = this.request.body;
				//尝试性登录
				var admin = AdminCon.login(form_data);
			}
			role.setAdmin(this, admin);
			this.body = ResponObj("json", admin);
		},
	}
}